package com.rocky.project.action.base;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JsonConfig;

import org.apache.log4j.Logger;

import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.Preparable;
import com.rocky.project.service.BaseService;
import com.rocky.project.util.JSONUtil;
import com.rocky.project.util.Page;
import com.rocky.project.util.Result;
import com.rocky.project.util.WebHelper;
import com.rocky.project.util.WebUtil;

/**
 * @author new
 *
 */
public class BaseAction  extends ActionSupport implements Preparable,EntityManagerActionIF {
	
	private static final long serialVersionUID = -8780734233485036368L;	
	public static final String ajaxResult="ajaxResult";	
	protected Logger log = Logger.getLogger(this.getClass());
	
	public Page page;
	public Collection pageRecords;	
	public Result result;	
	
	public BaseService baseService;
	
	public Map<String, Object> ajaxResultMap = new HashMap<String, Object>();
	public JsonConfig jsonConfig = JSONUtil.configJson(JSONUtil.DEFAULT_EXCLUDES, JSONUtil.DEFAULT_DATEPATTERN);
	public WebHelper webHelper = new WebHelper();

	
	public int pageSize = 10;
	public int start = 0;
	public int rowCount=0;	
	public String id;
	public String[] ids;
	public String jsondata;
	
	public String suc="";
	public String cmd;
	public String from;
	public String fromadmin;
	

	public String returnSucStr() throws Exception {
		if (suc == null || suc.equals("")) {
			return SUCCESS;
		}
		return suc;
	}
	public String ajaxerror() throws Exception {
		this.addActionMessage("很抱歉，操作有错误！");
		return ajaxResult;
	}	
	public String deleteProcess(Class cls) throws Exception {
		String[] ids = WebUtil.getParaValue("ids").split(",");
		for(String id :ids){		
			baseService.delete(baseService.get(cls, Long.valueOf(id)));
		}
		return ajaxResult;
	}
	public int getPageSize() {
		String limit = WebUtil.getParaValue("limit");
		if(limit!=null&&!limit.equals("")){
			pageSize=Integer.valueOf(limit);
		}		
		return pageSize;
	}
	private boolean systemEnable(){
		String systemEnable = WebUtil.getConfigValue("project.systemEnable");	
		if(systemEnable==null){
			systemEnable="true";
			 WebUtil.setConfigValue("project.systemEnable", systemEnable);			 
		}
		log.info("##############systemEnable:"+systemEnable+"\n");
		return Boolean.valueOf(systemEnable).booleanValue();
	}
	public void prepare() throws Exception {		
		//ServletActionContext.getContext().getActionInvocation();
		log.info("##prepare:" + this.cmd + "\n");
		if(!systemEnable()){
			this.addActionMessage("服务没有启动，请联系管理员！");	
			log.info("##############prepare:服务没有启动，请联系管理员！\n");
		}
//		User member =  WebUtil.getSessionUser();
//		if(member==null){
//			member =(Family) baseService.get(Family.class,1l);
//			if(member==null){
//				Person person= new Person();
//				person.setName("jianfeng008cn");
//				person.setType("Person");				
//				baseService.create(person);
//				Family family=new Family();
//				family.setUsername("jianfeng008cn");
//				family.setPassword("jianfeng008cn");
//				family.setType("Family");
//				family.setHouseholder(person);	
//				baseService.create(family);					
//				Archive archive = new Archive();				
//				BeanUtil.copyProperties(family, archive,BeanUtil.DEFAULT_EXCLUDES);			
//				archive.setType("Archive");					
//				archive.setFamily(family);
//				baseService.create(archive);
//				person.setFamily(family);
//				baseService.update(person);				
//				WebUtil.setSessionUser(family);		
//			}			
//		}
		
		log.info("##prepare##success:" + this.cmd + "\n");
	}	
	public String cmd() throws Exception {
		log.info("##cmd:" + this.cmd + "\n");
		if(this.cmd!=null && !this.cmd.equals("")){
			if(this.cmd.equals("read")){
				return read();
			}
			if(this.cmd.equals("readone")){
				return readOne();
			}			
			if(this.cmd.equals("create")){
				return createProcess();
			}
			if(this.cmd.equals("update")){
				return updateProcess();
			}
			if(this.cmd.equals("remove")){
				return deleteProcess();
			}
			if(this.cmd.equals("updatelink")){
				return updateLink();
			}
			if(this.cmd.equals("createlink")){
				return createLink();
			}			
		}
		log.info("##没有找到相应的操作!");
		return NONE;
	}
	
	public String getSuc() {
		return suc;
	}
	public void setSuc(String suc) {
		this.suc = suc;
	}
	public String getFromadmin() {
		return fromadmin;
	}
	public void setFromadmin(String fromadmin) {
		this.fromadmin = fromadmin;
	}

	public JsonConfig getJsonConfig() {
		return jsonConfig;
	}
	public void setJsonConfig(JsonConfig jsonConfig) {
		this.jsonConfig = jsonConfig;
	}
	public Map<String, Object> getAjaxResultMap() {
		return ajaxResultMap;
	}
	public void setAjaxResultMap(Map<String, Object> ajaxResultMap) {
		this.ajaxResultMap = ajaxResultMap;
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public BaseService getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}


	public String createLink() throws Exception {
		log.info("baseaction addLink");
		return returnSucStr();
	}

	public String createProcess() throws Exception {
		log.info("baseaction addProcess");
		return returnSucStr();
	}

	public String deleteProcess() throws Exception {
		log.info("baseaction deleteProcess");
		return returnSucStr();
	}

	public String updateLink() throws Exception {
		log.info("baseaction updateLink");
		return returnSucStr();
	}

	public String updateProcess() throws Exception {
		log.info("baseaction editProcess");
		return returnSucStr();
	}

	public String read() throws Exception {
		log.info("baseaction index");
		return returnSucStr();
	}

	public String readOne() throws Exception {
		log.info("baseaction view");
		return returnSucStr();
	}


	
	public Page getPage() {
		return page;
	}


	public void setPage(Page page) {
		this.page = page;
	}


	public String getJsondata() {
		return jsondata;
	}


	public void setJsondata(String jsondata) {
		this.jsondata = jsondata;
	}


	public int getRowCount() {
		return rowCount;
	}


	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}


	public String getFrom() {
		return from;
	}


	public void setFrom(String from) {
		this.from = from;
	}





	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public String getCmd() {
		return cmd;
	}


	public void setCmd(String cmd) {
		this.cmd = cmd;
	}

	public Collection getPageRecords() {
		return pageRecords;
	}

	public void setPageRecords(Collection pageRecords) {
		this.pageRecords = pageRecords;
	}

	public int getStart() {

		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}



	public WebHelper getWebHelper() {
		return webHelper;
	}

	public void setWebHelper(WebHelper webHelper) {
		this.webHelper = webHelper;
	}
	
	public String redirect() throws Exception{
		//log.info("baseaction redirect");
		return returnSucStr();
	} 



	
	

}
